<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

const animationDuration = 3000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        title: {
          text: this.$t("dashboard.educationDistribution"),
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          left: "center",
          bottom: "40",
          data: [
            this.$t("dashboard.juniorCollege"),
            this.$t("dashboard.undergraduate"),
            this.$t("dashboard.master"),
          ],
        },
        series: [
          {
            name: "Budget",
            type: "pie",
            radius: "50%",
            center: ["50%", "40%"],
            data: [
              { value: 5000, name: this.$t("dashboard.juniorCollege") },
              { value: 4000, name: this.$t("dashboard.undergraduate") },
              { value: 5500, name: this.$t("dashboard.master") },
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
            animationDuration: animationDuration,
          },
        ],
      });
    },
  },
};
</script>
